package dymnd.ui.color;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;

public class SwatchPanel extends JPanel{


    
    int rows = 13;
    int columns = 13;
    Color[] standardColors;
    Color[] grayColors;
    Color[] darkColors;
    Color[] miscColors;
    
    Robot robot;
    
    ActionListener actionListener;
    
    Color selectedColor = Color.black;
    public static final String defaultActionCommand = "SwatchPanel";
    String actionCommand = defaultActionCommand;
    
    private long lastClick = System.currentTimeMillis();
    
    private final int CLICK_GAP = 50;
    
    /** Creates new form SwatchPanel */
    public SwatchPanel() {
        createSwatches(rows, columns);
        
        try {
            robot = new Robot();
            
        }   catch(Exception x)  {
            x.printStackTrace();
        }
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e)  {
               selectedColor = robot.getPixelColor(e.getXOnScreen(), e.getYOnScreen());
               fireAction();
           }
           @Override
           public void mousePressed(MouseEvent e)   {
               if(System.currentTimeMillis() - lastClick <= CLICK_GAP)
                   showColorChooser();
           }
            
                   
        });
        
       
    }
    
    public Color getPrimaryColor()	{
		return selectedColor;
	}
    
    private void showColorChooser() {
        System.out.println("Not implemented yet");
    }
    
    public void setActionCommand(String s)  {
        actionCommand = s;
    }
    
    public String getActionCommand()  {
        return actionCommand;
    }
        
    private void fireAction()   {
        if(actionListener != null)  {
            ActionEvent e = new ActionEvent(this, 0, actionCommand);
            actionListener.actionPerformed(e);
        }
    }
    
    public void setActionListener(ActionListener l) {
        actionListener = l;
    }
   
    
    public void setRowsColumns(int r, int c)    {
        rows = r;
        columns = c;
        createSwatches(r, c);
        repaint();
    }
    
    
    
    public Color getSelectedColorPrimary()  {
        return selectedColor;
    }
    
    public Color getSelectedColorSecondary()    {
        return selectedColor;
    }
    
    
    
    
    
    
    @Override
    public void paintComponent(Graphics arg0)  { 
        Graphics2D g = (Graphics2D) arg0;
        float width = getWidth()/rows;
        float height = getHeight()/columns;
        
        int index = 0;
        for(float y = 0; y < columns; y++) {
            for(int x = 0; x < rows - 3; x++) {
                g.setColor(standardColors[index]);
                g.fill(new Rectangle2D.Float(x*width, y*height, width, height));
                index++;
            }
        }
        
        int x = rows - 1;
        for(float y = 0; y < columns; y++)    {
            g.setColor(grayColors[(int)y]);
            g.fill(new Rectangle2D.Float(x*width, y*height, width, height));
        }
        
        x = rows - 2;
        for(float y = 0; y < columns; y++)    {
            g.setColor(miscColors[(int)y]);
            g.fill(new Rectangle2D.Float(x*width, y*height, width, height));
        }
        
        x = rows - 3;
        for(float y = 0; y < columns; y++)    {
            g.setColor(darkColors[(int)y]);
            g.fill(new Rectangle2D.Float(x*width, y*height, width, height));
        }
        
        
    }
    
    
    private void createSwatches(int width, int height)   {
        
        float hue;
        float color;
        float lightness;
        
        //+ (float)(1.0/standardHeight)
        float standardWidth = width;
        float standardHeight = height - 3;
        int standardIndex = 0;
        standardColors = new Color[(int)(standardWidth*standardHeight)];
        //calculate the first batch of colors
        for(int row = 0; row < standardWidth; row++)    {
            hue = (float) 1.0/standardWidth*row;
            for(int column = 0; column < standardHeight; column++)  {
                color = (float) 1.0/standardHeight*column + (float)1.0/standardHeight;
                
                standardColors[standardIndex] = Color.getHSBColor(hue, color, 1f);
                standardIndex++;
            }
            
        }
        
        grayColors = new Color[1*height];
        hue = 0;
        color = 0;
        for(int column = 0; column < height; column++)  {
            lightness = 1.0f/height*column;
            grayColors[column] = Color.getHSBColor(hue, color, lightness);
        }
        
        darkColors = new Color[1*height];
        color = 1;
        for(int column = 0; column < height; column++)  {
            hue = 1.0f/height*column;
            darkColors[column] = Color.getHSBColor(hue, color, 0.8f);
        }
        
        miscColors = new Color[1*height];
        color = 1;
        for(int column = 0; column < height; column++)  {
            hue = 1.0f/height*column;
            miscColors[column] = Color.getHSBColor(hue, color, 0.5f);
        }
        
  
        /*for(int column = 0; column < standardWidth; column++)   {
            hue = (float) 1.0/standardWidth*column;
            for(int row = 0; row < standardHeight; row++)   {
                color = (float) 1.0/standardHeight*row;
                standardColors[standardIndex] = Color.getHSBColor(hue, color, 1f);
                standardIndex++;
            }
        }*/
       
    }
	
}
